package com.hand.zc25650.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hand.zc25650.pojo.Item;
import com.hand.zc25650.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Item> itemList(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,Item item){
        PageHelper.startPage(pageNo,pageSize,true);
        List<Item> itemList=itemService.selectAll();
        PageInfo<Item> pageInfo=new PageInfo(itemList,5);
        return pageInfo;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteItem(@RequestParam long itemId){
        return itemService.deleteItem(itemId);
    }

    @RequestMapping("/select")
    @ResponseBody
    public Item selectItem(@RequestParam long itemId){
        return itemService.selectItem(itemId);
    }

    @RequestMapping("/update")
    @ResponseBody
    public boolean selectItem(@Valid Item item, @RequestParam("enabledFlag_") String enabledFlag){
        if ("是".equals(enabledFlag)){
            item.setEnabledFlag((byte) 1);
        }else {
            item.setEnabledFlag((byte) 0);
        }
        return itemService.updateItem(item);
    }

    @RequestMapping("/add")
    @ResponseBody
    public boolean addItem(@Valid Item item, @RequestParam("enabledFlag_") String enabledFlag){
        if ("是".equals(enabledFlag)){
            item.setEnabledFlag((byte) 1);
        }else {
            item.setEnabledFlag((byte) 0);
        }
        boolean flag=itemService.addItem(item);
        return flag;
    }



    @RequestMapping("/deleteList")
    @ResponseBody
    public int deleteList(@RequestParam List<Long> idList){
        int count=0;
        for (Long i:idList){
            if (i!=null){
                itemService.deleteItem(i);
                count++;
            }
        }
        return count;
    }


}
